﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Casemeet.Members;

namespace VK.Casemeet.Web.Front
{
    public partial class MemberLogin : System.Web.UI.Page
    {
        public string strMessage = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            string surl = null;
            if (Request.Url != null) surl = Request.Url.ToString();
            if ((Request.UrlReferrer != null) && (Request.UrlReferrer.ToString()) != surl)
            {
                Session["Sites:Page:Url"] = Request.UrlReferrer.ToString();
            }
        }

        protected void butSubmit_Click(object sender, EventArgs e)
        {
            CheckMember(txtEmail.Text, txtPass.Text, chkState.Checked);
        }

        protected void CheckMember(string username, string password, bool isSave)
        {
            if (username.Length == 0)
            {
                strMessage += "<p>帐号不能为空!</p>";
            }
            //else
            //{
            //    if (!RegexStr.CheckEmail(txtEmail.Text))
            //    {
            //        strMessage += "<p>帐号只能为邮箱格式!</p>";
            //    }
            //}
            if (password.Length == 0)
            {
                strMessage += "<p>密码不能为空!</p>";
            }
            else
            {
                if (txtPass.Text.Length < 4 || txtPass.Text.Length > 20)
                {
                    strMessage += "<p>密　码(4-20)位字符!</p>";
                }
            }
            if (txtCode.Text.Trim().Length == 0)
            {
                strMessage += "<p>验证码不能为空!</p>";
            }
            else
            {
                if (Util.GetCookieString("AdminValidateCode") != txtCode.Text.ToLower())
                {
                    strMessage += "<p>验证码输入错误!</p>";
                }
            }

            if (!string.IsNullOrEmpty(strMessage))
            {
                strMessage = "<dd id='vcodeMsg' class='loginMsg'>" + strMessage;
                strMessage = strMessage + "</dd>";
                return;
            }

            switch (MemberBLL.Enter(username, DES.Encrypt(password, CS.MemberPassKey), isSave))
            {
                case 1:
                    if (Session["Sites:Page:Url"] != null)
                    {
                        Response.Redirect(Session["Sites:Page:Url"].ToString());
                    }
                    else
                    {
                        Response.Redirect("/");
                    }
                    break;
                case 2: strMessage = "<dd id='vcodeMsg' class='loginMsg'><p>帐号不存在!</p></dd>"; break;
                case 3: strMessage = "<dd id='vcodeMsg' class='loginMsg'><p>密码错误!</p></dd>"; break;
            }
        }
    }
}